#include "mainwindow.h"

#include <QSettings>
#include <QDebug>
#include <QToolBar>
#include <QPalette>
#include <QMenuBar>
#include <QFile>
#include <QCoreApplication>

#include "qads/dockmanager.h"
#include "qads/dockwidget.h"
#include "qads/dockiconprovider.h"

using namespace QAds;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QMenuBar *menubar = this->menuBar();
    QMenu *menu = menubar->addMenu(tr("view"));

    QAds::setConfigFlags(QAds::configFlags()
                         | QAds::TitleBarCloseButtonClosesTab
                         | QAds::FocusHighlighting
//                         | QAds::OpaqueUndocking
                         );

    m_manager = new DockManager(this);

    m_manager->iconProvider().registerCustomIcon(TitleBarTabsMenuIcon, QIcon(":/main/menu"));
    m_manager->iconProvider().registerCustomIcon(TitleBarUndockIcon, QIcon(":/main/undock"));
    m_manager->iconProvider().registerCustomIcon(TitleBarCloseIcon, QIcon(":/main/close"));

    QFile file(":/main/dockwidgets.css");
    if (file.open(QFile::ReadOnly)) {
        QString qss = QLatin1String(file.readAll());
        m_manager->setStyleSheet(qss);
        file.close();
    }

    DockWidget *dw = nullptr;
    QWidget *w = nullptr;

    dw = new DockWidget("test1", m_manager);
    menu->addAction(dw->toggleViewAction());
    w = new QWidget();
    dw->setWidget(w);
    m_manager->addDockWidget(LeftDockWidgetArea, dw);

    dw = new DockWidget("test2", m_manager);
    menu->addAction(dw->toggleViewAction());
    w = new QWidget();
    dw->setWidget(w);
    m_manager->addDockWidget(RightDockWidgetArea, dw);

    dw = new DockWidget("test3", m_manager);
    menu->addAction(dw->toggleViewAction());
    w = new QWidget();
    dw->setWidget(w);
    m_manager->addDockWidget(TopDockWidgetArea, dw);

    dw = new DockWidget("test4", m_manager);
    menu->addAction(dw->toggleViewAction());
    w = new QWidget();
    dw->setWidget(w);
    m_manager->addDockWidget(BottomDockWidgetArea, dw);

    dw = new DockWidget("test5", m_manager);
    menu->addAction(dw->toggleViewAction());
    w = new QWidget();
    dw->setWidget(w);
    m_manager->addDockWidget(CenterDockWidgetArea, dw);

    setCentralWidget(m_manager);

    resize(800, 600);

//    restoreState();
}

MainWindow::~MainWindow()
{
    saveState();
}

QString MainWindow::workspaceFileName() const
{
    QString appDirPath = QCoreApplication::applicationDirPath();
    appDirPath = appDirPath.replace("\\","/");
    return appDirPath + "/workspace.xml";
}

bool MainWindow::restoreState()
{
    QFile file(workspaceFileName());
    bool res = false;
    if (file.open(QFile::ReadOnly)) {
        QByteArray state = QByteArray(file.readAll());
        file.close();
        res = m_manager->restoreState(state);
    }
    return res;
}

void MainWindow::saveState()
{
    QFile file(workspaceFileName());
    if (file.open(QFile::WriteOnly)) {
        file.write(m_manager->saveState());
        file.close();
    }
}
